package com.hzhr.iot.web.constant;

import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.device.sdk.model.options.OptionsModel;
import com.hzhr.iot.device.sdk.service.Device_OptionsService;
import com.hzhr.iot.toll.sdk.service.Toll_OptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DeviceOptions implements CommandLineRunner {
    @Autowired
    Device_OptionsService device_optionsService;

    private Map<String, Map<String, Map<String, String>>> map;

    @Override
    public void run(String... strings) {
        map = new HashMap<>();

        try {
            Map<String, String> header = new HashMap<>();
            Map<String, Object> query = new HashMap<>();
            query.put("show", "list");
            query.put("show_info", true);
            List<OptionsModel> optionsModels = device_optionsService.get(header, query).getModelList();
            for (OptionsModel optionsModel:
                    optionsModels) {
                String typeName = optionsModel.getTypeName();
                Map<String, Map<String, String>> value = map.get(typeName);
                if (value == null) {
                    value = new HashMap<>();
                }
                Map<String, String> value1 = new HashMap<>();
                value1.put(optionsModel.getDes(), optionsModel.getCode());
                value.put(optionsModel.getName(), value1);
                map.put(typeName, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getCode(String typeName, String name) throws ServiceException {
        Map<String, Map<String, String>> item = map.get(typeName);
        if (item != null) {
            Map<String, String> value = item.get(name);
            for (Map.Entry<String, String> entry1:
                    value.entrySet()) {
                return entry1.getValue();
            }
        } else {
            Map<String, String> header = new HashMap<>();
            Map<String, Object> query = new HashMap<>();
            query.put("show", "list");
            query.put("show_info", true);
            List<OptionsModel> optionsModels = device_optionsService.get(header, query).getModelList();
            for (OptionsModel optionsModel:
                    optionsModels) {
                typeName = optionsModel.getTypeName();
                Map<String, Map<String, String>> value = map.get(typeName);
                if (value == null) {
                    value = new HashMap<>();
                }
                Map<String, String> value1 = new HashMap<>();
                value1.put(optionsModel.getDes(), optionsModel.getCode());
                value.put(optionsModel.getName(), value1);
                map.put(typeName, value);
            }

            item = map.get(typeName);
            if (item != null) {
                Map<String, String> value = item.get(name);
                for (Map.Entry<String, String> entry1:
                        value.entrySet()) {
                    return entry1.getValue();
                }
            }
        }
        return null;
    }

    public String getDesByCode(String typeName, String code) throws ServiceException {
        Map<String, Map<String, String>> item = map.get(typeName);
        if (item != null) {
            for (Map.Entry<String, Map<String, String>> entry :
                    item.entrySet()) {
                Map<String, String> value = entry.getValue();

                for (Map.Entry<String, String> entry1:
                        value.entrySet()) {
                    String des = entry1.getKey();
                    if (entry1.getValue().equals(code)) {
                        return des;
                    }
                }
            }
        } else {
            Map<String, String> header = new HashMap<>();
            Map<String, Object> query = new HashMap<>();
            query.put("show", "list");
            query.put("show_info", true);
            List<OptionsModel> optionsModels = device_optionsService.get(header, query).getModelList();
            for (OptionsModel optionsModel:
                    optionsModels) {
                typeName = optionsModel.getTypeName();
                Map<String, Map<String, String>> value = map.get(typeName);
                if (value == null) {
                    value = new HashMap<>();
                }
                Map<String, String> value1 = new HashMap<>();
                value1.put(optionsModel.getDes(), optionsModel.getCode());
                value.put(optionsModel.getName(), value1);
                map.put(typeName, value);
            }

            item = map.get(typeName);
            if (item != null) {
                for (Map.Entry<String, Map<String, String>> entry :
                        item.entrySet()) {
                    Map<String, String> value = entry.getValue();

                    for (Map.Entry<String, String> entry1:
                            value.entrySet()) {
                        String des = entry1.getKey();
                        if (entry1.getValue().equals(code)) {
                            return des;
                        }
                    }
                }
            }
        }

        return null;
    }
}
